package com.mindether.llyblog.hanlder;

import com.mindether.llyblog.controller.ArticleController;
import com.mindether.llyblog.pojo.Tag;
import com.mindether.llyblog.service.TagService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

//@ControllerAdvice
//@Controller
//@Order(Ordered.HIGHEST_PRECEDENCE)
//@RequestMapping("${server.error.path}")

//@ControllerAdvice
public class ExceptionHanlder {
    private static final Logger log= LoggerFactory.getLogger(ExceptionHanlder.class);
    @Autowired
    private TagService tagService;
    @ExceptionHandler(Exception.class)
    @RequestMapping
    public String baseHanlder(Exception ex,Model model){
        List<Tag> tags=ArticleController.allTags;
        model.addAttribute("allTags",tags);
        log.error("出现错误，详细信息为：{}",ex.getMessage());
        return "/404";
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String notFoundHanlder(Exception ex,Model model){
        if(ex instanceof NoHandlerFoundException) {
            List<Tag> tags = ArticleController.allTags;
            model.addAttribute("allTags", tags);
            log.error("错误，详细信息为：{}", ex.getMessage());
        }
        return "/404";
    }

}
